<template>
    <div class="loginContainer">
        <form class="loginForm">
            <section class="input_container">
                <input type="text" placeholder="账号" v-model.lazy="userAccount">
            </section>
            <section class="input_container">
                <input v-if="!showPassword" type="password" placeholder="密码" v-model="passWord">
                <input v-else type="text" placeholder="密码" v-model="passWord">
                <div class="button_switch" :class="{change_to_text: showPassword}">
                    <div class="circle_button" :class="{trans_to_right: showPassword}"
                         @click="changePassWordType"></div>
                    <span>abc</span>
                    <span>...</span>
                </div>
            </section>
            <section class="input_container captcha_code_container">
                <input type="text" placeholder="验证码" maxlength="4" v-model="codeNumber">
                <div class="img_change_img">
                    <img v-show="captchaCodeImg" :src="captchaCodeImg">
                    <div class="change_img" @click="getCaptchaCode">
                        <p>看不清</p>
                        <p>换一张</p>
                    </div>
                </div>
            </section>
        </form>
        <p class="login_tips">
            温馨提示：未注册过的账号，登录时将自动注册
        </p>
        <p class="login_tips">
            注册过的用户可凭账号密码登录
        </p>
        <div class="login_container" @click="mobileLogin">登录</div>
    </div>
</template>

<script>
import { Dialog } from 'vant'
import { mapMutations } from 'vuex'
export default {
  name: 'index',
  data () {
    return {
      showPassword: false,
      userInfo: null,
      userAccount: null,
      passWord: null,
      captchaCodeImg: null, // 验证码地址
      codeNumber: null
    }
  },
  created () {
    this.getCaptchaCode()
  },
  methods: {
    ...mapMutations([
      'RECORD_USERINFO'
    ]),
    // 是否显示密码
    changePassWordType () {
      this.showPassword = !this.showPassword
    },
    async getCaptchaCode () {
      // eslint-disable-next-line no-unused-vars
      const res = await this.$http.getcaptchas()
      this.captchaCodeImg = res.data.code
    },
    async mobileLogin () {
      this.userInfo = await this.$http.login(this.userAccount, this.passWord, this.codeNumber)
      if (!this.userInfo.data.user_id) {
        Dialog.alert({ message: `${this.userInfo.data.message}` })
      } else {
        this.RECORD_USERINFO(this.userInfo.data)
        this.$router.go(-1)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
    @import "../../assets/profile/mixin.scss";

    .loginContainer {
        .loginForm {
            margin-top: 20px;

            .input_container {
                width: 100%;
                height: 54px;
                padding-top: 10px;
                background: #ffffff;
                border-bottom: 1px solid #f1f1f1;
                display: flex;

                input {
                    font-size: 20px;
                    display: block;
                    justify-content: space-between;
                    margin-left: 15px;
                    border: none;
                    outline: none;
                }

                .button_switch {
                    margin-left: 200px;
                    margin-top: 8px;
                    font-size: 30px;
                    background: #ccc;
                    display: flex;
                    width: 72px;
                    height: 25px;
                    justify-content: center;

                    border-radius: 15px;
                    position: relative;

                    .circle_button {
                        transition: all .3s;
                        position: absolute;
                        top: -10px;
                        z-index: 1;
                        left: -15px;
                        width: 42px;
                        height: 42px;
                        border-radius: 50%;
                        box-shadow: 0 0.026667rem 0.053333rem 0 rgba(0, 0, 0, .1);
                        background: #f1f1f1;
                    }

                    .trans_to_right {
                        transform: translateX(60px);
                    }

                    span {
                        font-size: 18px;
                        color: #ffffff;
                        line-height: 27px;
                    }

                }

                .change_to_text {
                    background-color: #4cd964;
                }

                .img_change_img {
                    margin-left: 100px;
                    display: flex;

                    .change_img {
                        width: 74px;
                        height:28px;
                        font-size: 18px;
                        display: flex;
                        flex-direction: column;
                        p{
                            margin: 0;
                            padding: 0;
                            width: 100px;
                        }
                        p:nth-of-type(2){
                            color: #3b95e9;
                            margin-top: .2rem;
                            margin: 0;
                        }
                    }
                }
            }
        }
        .login_tips{
            font-size: 20px;
            color: red;
            padding: 20px 30px;
            line-height: 30px;
            margin: 0;
        }
        .login_container{
            width: 530px;
            height: 73px;
            line-height: 73px;
            text-align: center;
            color: #ffffff;
            margin: 0 auto 20px;
            background:#4cd964 ;
        }
    }
</style>
